var request = require('request');
var config = require('../../config.json');
var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;

exports.addMember = function (req, resp, next) {
    var member_info = req.body;
    var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;
    request.post(sdp_server + "/api/member",
        {form: member_info},
        function (err, response, data) {
            if (err) {
                logger.error("添加成员失败");
                logger.error(err);
                return resp.format_json(null, 500, "添加成员失败!");
            } else {
                resp.format_json(data, 0);
            }
        });

};

/**
 * 删除成员时 检查是否有工作空间 有的话 删除
 * @param req
 * @param res
 * @param next
 */
exports.removeMember = function (req, res, next) {
    var body = req.body;
    var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;
    request(
        {
            url: sdp_server + '/api/member',
            method: "DELETE",
            json: true,
            body: body
        }
        , function (err, response, data) {
            if (err) {
                return res.format_json(null, 500, "删除成员失败:" + err);
            }
            res.format_json(data);
        });
};

exports.updateMember = function (req, res, next) {
    var body = req.body;
    request(
        {
            url: sdp_server + '/api/member',
            method: "PUT",
            json: true,
            body: body
        }
        , function (err, response, data) {
            if (err) {
                return res.format_json(null, 500, "更新成员信息失败:" + err);
            }
            res.format_json(data, 0);
        });
};

exports.getMembersByshoutids = function (req, res, next) {
    var shortid_arr = JSON.stringify(req.body.shortid);
    var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;
    request(
        {
            url: sdp_server + '/api/members',
            method: "POST",
            json: true,
            body: req.body.shortid
        }
        , function (err, response, data) {
            if (err) {
                return res.format_json(null, 500, "更新依赖失败:" + err);
            }
            res.format_json(data.data);
        });
};